package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ClientBean;
import com.project.mapper.IClientMapper;
import com.project.service.IClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 客户实现类
 */
@Service
@Transactional
public class ClientServiceImpl implements IClientService {
    @Autowired
    private IClientMapper clientMapper;
    @Override
    public IPage<ClientBean> cutByItem(Integer pageNO, String clientName, String manageRange) {
        QueryWrapper<ClientBean> qw=new QueryWrapper<>();
        if (clientName !=null && clientName.length() !=0){
            qw.like("c_clientName",clientName);
        }
        if (manageRange !=null && manageRange.length() !=0){
            qw.like("c_manageRange",manageRange);
        }
        return clientMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public ClientBean findClientById(Integer clientId) {
        return clientMapper.selectById(clientId);
    }

    @Override
    public void updateById(Integer clientId,String name, String phone,String manageRange) {
       ClientBean clientObj=clientMapper.selectById(clientId);
       clientObj.setPrincipalName(name);
       clientObj.setPhone(phone);
       clientObj.setManageRange(manageRange);
       clientMapper.updateById(clientObj);
    }

    @Override
    public void addClient(ClientBean client) {
        QueryWrapper<ClientBean> qw=new QueryWrapper<>();
        qw.eq("c_clientName",client.getClientName());
        ClientBean clientObj=clientMapper.selectOne(qw);
        if (clientObj==null){
            clientMapper.insert(client);
        }
    }

    @Override
    public List<ClientBean> findAllClient() {
        return clientMapper.selectList(null);
    }
}
